<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!curAdress">
        您需要先添加收货地址才可提交订单。
      </div>
      <ul v-else>
        <li>
          <span>收<i />货<i />人：</span>{{ curAdress.receiver }}
        </li>
        <li><span>联系方式：</span>{{ curAdress.contact }}</li>
        <li>
          <span>收货地址：</span
          >{{ curAdress.fullLocation.replace(/ /g, "") + curAdress.address }}
        </li>
      </ul>
      <a href="javascript:;" @click="editAddress">修改地址</a>
    </div>
    <div class="action">
      <XtxButton class="btn" @click="dialogVisible = true">切换地址</XtxButton>
      <XtxButton class="btn" @click="addAddress">添加地址</XtxButton>
    </div>
  </div>

  <XtxDialog title="切换收货地址" v-model:visible="dialogVisible">
    <div
      class="text item"
      v-for="item in userAddresses"
      :key="item.id"
      @click="selectedAddress = item"
      :class="{ active: selectedAddress === item }"
    >
      <ul>
        <li>
          <span>收<i />货<i />人：</span>{{ item.receiver }}
        </li>
        <li><span>联系方式：</span>{{ item.contact }}</li>
        <li><span>收货地址：</span>{{ item.fullLocation }}</li>
      </ul>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right: 20px">取消</XtxButton>
      <XtxButton type="primary" @click="modifyAddress">确认</XtxButton>
    </template>
  </XtxDialog>

  <AddressBox ref="target" :isEdit="isEdit" @on-success="hSuccess" />
</template>
<script>
import AddressBox from './checkout-address-box.vue'
import { ref } from 'vue'
export default {
  name: 'CheckoutAddress',
  components: {
    AddressBox
  },
  props: {
    userAddresses: {
      type: Array,
      default: () => ([])
    }
  },
  setup (props, { emit }) {
    // 当前地址
    const curAdress = ref(null)
    // 设置默认地址
    let defaultAddress = props.userAddresses.find(it => it.isDefault)
    if (!defaultAddress) {
      if (props.userAddresses.length > 0) {
        // eslint-disable-next-line vue/no-setup-props-destructure
        defaultAddress = props.userAddresses[0]
      }
    }
    curAdress.value = defaultAddress
    emit('change', defaultAddress)

    // 添加地址
    const target = ref(null)
    const addAddress = () => {
      target.value.open()
    }
    // 编辑地址
    const isEdit = ref(false)
    const editAddress = () => {
      isEdit.value = true
      target.value.open()
    }

    // 操作成功
    const hSuccess = (address) => {
      if (isEdit.value) {

      } else {
        // 引用类型可以修改
        // eslint-disable-next-line vue/no-mutating-props
        props.userAddresses.unshift(address)
      }
    }
    // 切换地址
    const dialogVisible = ref(false)
    // 选中地址高亮
    const selectedAddress = ref(null)
    // 确认切换地址
    const modifyAddress = () => {
      // 通知父组件
      emit('change', selectedAddress.value)
      // 更新当前地址
      curAdress.value = selectedAddress.value
      // 关闭弹层
      dialogVisible.value = false
    }
    return { curAdress, addAddress, target, isEdit, editAddress, hSuccess, dialogVisible, selectedAddress, modifyAddress }
  }
}
</script>
<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
// 覆盖样式
.xtx-dialog ::v-deep .wrapper .body {
  height: 400px;
  overflow: auto;
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
</style>
